package com.li.controller;


import com.li.pojo.dto.DictDateDto;
import com.li.pojo.dto.DictDatePageDto;
import com.li.pojo.po.DictDate;
import com.li.pojo.po.PageResult;
import com.li.pojo.po.Result;
import com.li.service.DictDateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//字典数据管理
@Slf4j
@RestController
@RequestMapping("/system/dict/data")
public class DictDateController {
    @Autowired
    private DictDateService dictDateService;

    //根据字典类型查询字典内容
    @GetMapping("/type/{dictType}")
    public Result getByDictType(@PathVariable String dictType){
        List<DictDate> list=dictDateService.findByDictType(dictType);
        if (list.isEmpty()){
            return new Result(404,"字典数据未添加,请联系管理添加",null);
        }
        return new Result(200,"操作成功",list);
    }

    //添加
    @PostMapping
    public Result addDictDate(@RequestBody DictDateDto dictDateDto){
        log.info("{}",dictDateDto);
        dictDateService.add(dictDateDto);
        return new Result(200,"添加字典数据成功",null);
    }

    //分页查询
    @GetMapping("/list")
    public PageResult findByPage(DictDatePageDto pageDto){
        log.info("{}",pageDto);
        PageResult page=dictDateService.findPage(pageDto);
        return page;
    }

    //根据ids删除字典数据
    @DeleteMapping("/{dictIds}")
    public Result deleteByIds(@PathVariable List<Integer> dictIds){
        log.info("删除ids:{}",dictIds);
        dictDateService.deleteByIds(dictIds);
        return new Result(200,"删除成功",null);
    }

    //根据dictCode查询字典
    @GetMapping("/{dictCode}")
    public Result findByDictCode(@PathVariable Integer dictCode){
        DictDate dictDate=dictDateService.findByDictCode(dictCode);
        return new Result(200,"查询成功",dictDate);
    }

    //修改字典数据
    @PutMapping
    public Result updateDictDate(@RequestBody DictDateDto dictDateDto){
        dictDateService.updateDictDate(dictDateDto);
        return new Result(200,"修改成功",null);
    }
}
